﻿using Labthink.PCSoft.UniversalHost4.UserControls.MsgBox;
using Labthink.PCSoft.UniversalHost4.Utility;
using Report.Model;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Labthink.PCSoft.UniversalHost4.Model;

namespace Report.Forms
{
    public partial class FrmAddCurve
    {
        private readonly ReportManager1 _manager;

        public FrmAddCurve()
        {
            InitializeComponent();
            Resources.MergedDictionaries.Add(ReportCommon.GetLan);
            _manager = ReportManager1.Create();
            BtnOk.Focus();
        }

        private void FrmAddCurve_OnLoaded(object sender, RoutedEventArgs e)
        {
            var datas = _manager.DataInfo;
            var xs = datas.Where(d => d.DataNo != 10034 && d.DataType != DataType.String && d.DataType != DataType.DateTime);
            CbbX.ItemsSource = xs;
            CbbX.DisplayMemberPath = "Text";
            CbbX.SelectedValuePath = "DataNo";
            _initY();
        }

        private void _initY()
        {
            foreach (var control in WpYs.Children)
            {
                var cbc = control as CheckBox;
                cbc.Checked -= Cb_Checked;
            }
            WpYs.Children.Clear();
            var sx = CbbX.SelectedValue?.ToInt() ?? 0;
            var ys = _manager.DataInfo.Where(d => d.DataNo != 10034 && d.DataNo != sx && d.DataType != DataType.String && d.DataType != DataType.DateTime);
            var style = Resources["CheckBoxStyle"] as Style;
            foreach (var y in ys)
            {
                var cb = new CheckBox { Content = y.Text, Margin = new Thickness(10, 5, 10, 5), Tag = y.DataNo };
                cb.Checked += Cb_Checked;
                if (style != null) cb.Style = style;
                WpYs.Children.Add(cb);
            }
        }

        private void Cb_Checked(object sender, RoutedEventArgs e)
        {
            if (sender is CheckBox cb)
            {
                if (cb.IsChecked.HasValue && cb.IsChecked.Value)
                {
                    var c = 0;
                    foreach (var control in WpYs.Children)
                    {
                        var cbc = control as CheckBox;
                        if (cbc?.IsChecked != null && cbc.IsChecked.Value)
                        {
                            c++;
                        }
                    }

                    if (c > 2) cb.IsChecked = false;
                }
            }
        }

        private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(TxtName.Text))
            {
                TxtName.BorderBrush = Brushes.Red;
                return;
            }
            if (TxtName.Text.Length > 50) TxtName.Text = TxtName.Text.Substring(0, 50);

            var ys = new List<Axis>();
            foreach (var control in WpYs.Children)
            {
                var cb = control as CheckBox;
                if (cb?.IsChecked != null && cb.IsChecked.Value)
                {
                    ys.Add(new Axis { Id = cb.Tag.ToInt() });
                }
            }
            if (ys.Count == 0) return;
            if (ys.Count > 5)
            {
                MyMessageBox.Show(this,Resources.GetLanguageValue("YAxisIsMore"));
                return;
            }
            // ReSharper disable once PossibleInvalidOperationException
            var r = _manager.AddCurves(TxtName.Text, RbShow.IsChecked.Value, new Axis { Id = CbbX.SelectedValue.ToInt() }, ys.ToArray());
            if (r < 0)
            {
                MyMessageBox.Show(this,Resources.GetLanguageValue("NameIsExist"));
                return;
            }
            DialogResult = true;
            Close();
        }

        private void CbbX_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (CbbX.SelectedValue != null)
            {
                _initY();
            }
        }

        private void TxtName_OnGotFocus(object sender, RoutedEventArgs e)
        {
          
        }

        private void TxtName_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
           
        }
    }
}
